<%@ page contentType="text/html; charset=UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="ru.ifmo.rybakov.muclumon.Database"%>
<%@page import="javax.persistence.Query"%>
<%@page import="java.util.List"%>
<%@page import="ru.ifmo.rybakov.muclumon.Revision"%>
<%@page import="ru.ifmo.rybakov.muclumon.Element"%>
<%@page import="ru.ifmo.rybakov.muclumon.StringUtils"%>
<%@page import="ru.ifmo.rybakov.muclumon.PropertyValue"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="ru.ifmo.rybakov.muclumon.RevisionType"%>
<%@page import="ru.ifmo.rybakov.muclumon.Task"%><html>
<head>
	<title>MUCLUMON</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta http-equiv="Pragma" content="no-cache">
	<link type="text/css" rel="stylesheet" href="css/style.css"></link> 
	<script type="text/javascript" src="<c:url value="/script/yui/yahoo/yahoo-min.js"/>"></script> 
	<script type="text/javascript" src="<c:url value="/script/yui/event/event-min.js"/>"></script> 
	<script type="text/javascript" src="<c:url value="/script/yui/connection/connection-min.js"/>"></script>
	<script type="text/javascript" src="<c:url value="/script/yui/dom/dom-min.js"/>"></script>
	<script type="text/javascript">
		function lr(id) { // loads the revision data by id
			YAHOO.util.Connect.asyncRequest('GET', '<c:url value="/revision/?id=" />' + id, {
				success : function(o) { YAHOO.util.Dom.get('r' + id).innerHTML = o.responseText; },
				failure : function(o) { alert(o.responseText); }
			});
		}
		function cr(id) { // clears the revision data by revision id
			YAHOO.util.Dom.get('r' + id).innerHTML = "";
		}
		function rp(rid, pid) { // removes property value by property value id
			YAHOO.util.Connect.asyncRequest('POST', '<c:url value="/property/?action=remove&id=" />' + pid, {
				success : function(o) { YAHOO.util.Dom.get('r' + rid).innerHTML = o.responseText; },
				failure : function(o) { alert(o.responseText); }
			});
		}
		function ap(eid,rid) { // adds the property for element (eid is element id)
			var name = YAHOO.util.Dom.get('pn' + eid).value;
			var value = YAHOO.util.Dom.get('pv' + eid).value;
			if (!name) {
				name = YAHOO.util.Dom.get('pl' + eid).value;
			}
			if (!value) {
				if(!confirm("Задать значение в виде пустой строки?")) return;
				else value = "";
			}

			// now send request to servlet
			YAHOO.util.Connect.asyncRequest('POST', '<c:url value="/property/?action=add&id=" />' + eid + '&name=' + name + '&value=' + value, {
				success : function(o) { YAHOO.util.Dom.get('r' + rid).innerHTML = o.responseText; },
				failure : function(o) { alert(o.responseText); }
			});
			// alert(name + "," + value);
		}
	</script>
</head>
<body>
<%
	EntityManager em = Database.getEntityManagerFactory().createEntityManager();
	try {
		Query revisionQuery = em.createQuery("SELECT r FROM Revision r");
		List<Revision> revisions = revisionQuery.getResultList();
		for (Revision r : revisions) {
%>
<sub>
	<a onclick='lr(<%=r.getId()%>);'>view</a> / <a onclick='cr(<%=r.getId()%>);'>hide</a>
</sub>
<%
	if (r.getType() == RevisionType.TASK_CONFIGURATION) {
		Query taskQuery = em.createQuery("SELECT t FROM Task t WHERE t.revision = :revision").setParameter("revision", r);
		Task task = (Task) taskQuery.getSingleResult();
		out.print("[" + task.getForeignId() + "]");
	}
%>
<%=r.getTimestamp().toString()%> (<%=r.getType()%>)<br/>
<div id='r<%=r.getId()%>'></div>
<%
			
		}
	} finally {
		em.close();
	}
%>
</body>
</html>